library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;

entity debouncer is
	generic ( BounceCount: natural range 1 to 100 := 100);
	port (
		Clock: in std_logic;
		Orig: in std_logic;
		Debounced: out std_logic
	);
end entity debouncer;

architecture beh of debouncer is
begin
	process (Clock)
		variable count: natural range 0 to BounceCount;
		variable current_state: std_logic := '0';
	begin
		if Clock'event and Clock = '1' then
			if Orig /= current_state then
				count := count + 1;
			end if;
			if count = BounceCount then
				current_state := Orig;
				count := 0;
			end if;
		end if;
		
		Debounced <= current_state;
	end process;
end architecture beh;